package com.lxw.library.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.lxw.library.R;


public class SubmitDialog extends Dialog {
    public interface ClickListener {
        void toConfirm();

        void toRefause();
    }

    public SubmitDialog(Context context) {
        super(context);
        setContentView(R.layout.dialog_submit);
    }

    private void disPaly(String title, String message, String lefttext, String righttext, final ClickListener mListener) {
        TextView titleTv = findViewById(R.id.titleTv);
        TextView cancelBtn = findViewById(R.id.cancelbtn);
        TextView callbtn = findViewById(R.id.callbtn);
        TextView messagetext = findViewById(R.id.message);
        View lineV = findViewById(R.id.lineV);

        titleTv.setText(title);
        messagetext.setText(message);
        if (TextUtils.isEmpty(lefttext)) {
            cancelBtn.setVisibility(View.GONE);
            lineV.setVisibility(View.GONE);
        }
        if (TextUtils.isEmpty(righttext)) {
            callbtn.setVisibility(View.GONE);
            lineV.setVisibility(View.GONE);
        }
        cancelBtn.setText(lefttext);
        callbtn.setText(righttext);

        callbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mListener) {
                    mListener.toConfirm();
                }

            }
        });
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mListener) {
                    mListener.toRefause();
                }
            }
        });
    }


    @Override
    public void dismiss() {
        super.dismiss();
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    public static class Builder {
        SubmitDialog submitDialog;
        String title, message, leftBtn, rightBtn;
        private ClickListener mListener;

        public SubmitDialog build(Context context) {
            return new SubmitDialog(context);
        }

        public SubmitDialog buildTitle(String title) {
            this.title = title;
            return submitDialog;
        }

        public SubmitDialog buildMsg(String message) {
            this.message = message;
            return submitDialog;
        }

        public SubmitDialog buildLeftTxt(String txt) {
            this.leftBtn = txt;
            return submitDialog;
        }

        public SubmitDialog buildRightTxt(String txt) {
            this.rightBtn = txt;
            return submitDialog;
        }

        public SubmitDialog bulidListener(ClickListener listener) {
            this.mListener = listener;
            return submitDialog;
        }

        public void disPlay() {
            submitDialog.disPaly(title, message, leftBtn, rightBtn, mListener);
        }
    }

}
